package com.robusta.cityuate.objects;

import java.io.Serializable;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.robusta.cityuate.services.CheckInOutService;
import com.robusta.cityuate.services.DeleteReservationService;
import com.robusta.cityuate.services.HTTPService;
import com.robusta.cityuate.utils.ConnectionUtils;
import com.robusta.cityuate.utils.TimeUtils;
import com.robusta.cityuate.utils.Token;

@SuppressWarnings("serial")
public class Reservation implements Serializable {

	public static final String RESERVATION = "reservation";
	public static final String RESERVATION_CHECK_IN_AT = "check_in_at";
	public static final String RESERVATION_CHECK_OUT_AT = "check_out_at";
	public static final String RESERVATION_ID = "id";
	Date checkInTime;
	Date checkoutTime;

	public static final String RESERVATION_SID = "sid";
	public static final String RESERVATION_CHECK_IN_AT_1I = "check_in_at(1i)";
	public static final String RESERVATION_CHECK_IN_AT_2I = "check_in_at(2i)";
	public static final String RESERVATION_CHECK_IN_AT_3I = "check_in_at(3i)";
	public static final String RESERVATION_CHECK_IN_AT_4I = "check_in_at(4i)";
	public static final String RESERVATION_CHECK_IN_AT_5I = "check_in_at(5i)";
	public static final String RESERVATION_CHECK_OUT_AT_1I = "check_out_at(1i)";
	public static final String RESERVATION_CHECK_OUT_AT_2I = "check_out_at(2i)";
	public static final String RESERVATION_CHECK_OUT_AT_3I = "check_out_at(3i)";
	public static final String RESERVATION_CHECK_OUT_AT_4I = "check_out_at(4i)";
	public static final String RESERVATION_CHECK_OUT_AT_5I = "check_out_at(5i)";
	

	public Date getCheckInTime() {
		return checkInTime;
	}

	public void setCheckInTime(Date checkInTime) {
		this.checkInTime = checkInTime;
	}

	public Date getCheckoutTime() {
		return checkoutTime;
	}

	public void setCheckoutTime(Date checkoutTime) {
		this.checkoutTime = checkoutTime;
	}

	public User getReserver() {
		return reserver;
	}

	public void setReserver(User reserver) {
		this.reserver = reserver;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	User reserver;
	long id;
	Spot spot;

	public Spot getSpot() {
		return spot;
	}

	public void setSpot(Spot spot) {
		this.spot = spot;
	}

	public static Reservation fromJSON(JSONObject reservationJSON) {
		Reservation res = new Reservation();
		try {
			JSONObject resData = reservationJSON.getJSONObject(RESERVATION);
			
			res.checkInTime = TimeUtils.getDateTimeFromString(
					resData.getString(RESERVATION_CHECK_IN_AT),
					Offering.DATE_TIME_FORMAT);
			res.checkoutTime = TimeUtils.getDateTimeFromString(
					resData.getString(RESERVATION_CHECK_OUT_AT),
					Offering.DATE_TIME_FORMAT);
			if(resData.has(User.RESERVER))
				res.reserver = User.fromJSON(resData);
			if(resData.has(Spot.SPOT))
				res.spot = Spot.createFromJSON(resData);
			
			
			res.id = resData.getLong(RESERVATION_ID);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return res;

	}

	public void delete(Context mContext, String delTag) {
		if (!ConnectionUtils.isNetworkAvailable(mContext))// {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);

		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext,
				DeleteReservationService.class);
		backgroud_regService.setAction(delTag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(RESERVATION_ID, this.id);
		mContext.startService(backgroud_regService);
		Log.e(delTag, "Fired DeleteReservation service");

	}

	public void checkIn(Context mContext, String tag) {
		fireCheckInOutService(mContext, tag, true);
	}

	private void fireCheckInOutService(Context mContext, String tag,
			boolean isCheckIn) {
		if (!ConnectionUtils.isNetworkAvailable(mContext))// {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);

		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext,
				CheckInOutService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(RESERVATION, this);
		backgroud_regService.putExtra(CheckInOutService.IS_CHECK_IN, isCheckIn);
		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired CheckInOut service");
	}

	public static Reservation restoreReservation(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				Token.SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		String jsonRes = sharedPreferences.getString(RESERVATION, null);
		if (jsonRes == null)

			return null;
		else {
			Gson gson = new Gson();
			return gson.fromJson(jsonRes, Reservation.class);
		}
	}

	public static void saveReservation(Context context, Reservation reservation) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				Token.SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		Gson gson = new Gson();
		String json = gson.toJson(reservation);
		editor.putString(RESERVATION, json);
		editor.commit();

	}

	public static void removeReservation(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				Token.SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(RESERVATION);
		editor.commit();

	}

	public void checkOut(Context mContext, String tag) {
		fireCheckInOutService(mContext, tag, false);
	}
}
